﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Gadgets.Banks
{
    public partial class eDepositWithdraw : WeifenLuo.WinFormsUI.Docking.DockContent  
    {
        public eDepositWithdraw()
        {
            InitializeComponent();
        }

        private void eDepositWithdraw_Load(object sender, EventArgs e)
        {
            try
            {
                ABSCommonLibrary.CreateGridView(ref gridControl1);
                DataSet tran_type = ABSCommonLibrary.DoInquiry(
                            CommonLibrary.CommonLibrary.BOSC014002, new object[3] { 4, 0, 0 });
                DataSet tran_type1 = tran_type.Copy();
                cboTransType.DataSource = tran_type.Tables[0];
                cboTransType.DisplayMember = "DESCRIPTION";
                cboTransType.ValueMember = "CODE";

                comboBoxViewer1.SetDataBinding(tran_type1, tran_type1.Tables[0].TableName);
                comboBoxViewer1.ValueMember = "CODE";
                comboBoxViewer1.DisplayFormat = "%DESCRIPTION%";
                gridControl1.Columns["TRANS_TYPE"].CellViewerManager = comboBoxViewer1;

                colTRANS_DATE.FormatSpecifier = DeclareLibrary.DATE_DISPLAY_FORMAT;
                colAMOUNT.FormatSpecifier = DeclareLibrary.NUMBER_FORMAT;
                colDATE_CREATED.FormatSpecifier = DeclareLibrary.DATE_DISPLAY_FORMAT;
            }
            catch (Exception ex)
            {
                ABSCommonLibrary.ShowErrorMessage(ex.ToString());
            }
             
        }

        private void load_grid()
        {
            //if (txtAccountNo.Value.Trim().Length != 10)
            //{
            //    ABSCommonLibrary.CreateGridView(ref gridControl1);
            //    return;
            //}            
            DataSet v_data = ABSCommonLibrary.DoInquiry(
                CommonLibrary.CommonLibrary.BOSC014007, new object[4]{
                    DeclareLibrary.MARKET_DAY, txtAccountNo.Value, 0, string.Empty});
            gridControl1.SetDataBinding(v_data, v_data.Tables[0].TableName);
        }

        private void txtAccountNo_Validated(object sender, EventArgs e)
        {
            try
            {
                DataSet dtsCashInfo = ABSCommonLibrary.DoInquiry(
                    CommonLibrary.CommonLibrary.BOSC004007, new object[1] { txtAccountNo.Text });
                
                if (dtsCashInfo.Tables[0].Rows.Count > 0)
                {
                    DataRow _row = dtsCashInfo.Tables[0].Rows[0];
                    nbrTotalAmount.Text =
                        Convert.ToDecimal(_row["total_amount"]).ToString(DeclareLibrary.CURRENCY_FORMAT);
                    nbrUnsettleAmount.Text =
                        (Convert.ToDecimal(_row["locked_amount"])
                        + Convert.ToDecimal(_row["fee_amount"])).ToString(DeclareLibrary.CURRENCY_FORMAT);                    
                    nbrWithdrawAmount.Text=(Convert.ToDecimal(_row["total_amount"]) - Convert.ToDecimal(_row["fee_amount"])
                         - Convert.ToDecimal(_row["locked_amount"])).ToString(DeclareLibrary.CURRENCY_FORMAT);

                }
                load_grid();
            }
            catch (Exception ex)
            {
                ABSCommonLibrary.ShowErrorMessage(ex.ToString());
            }
        }

        private void btnAddNew_Click(object sender, EventArgs e)
        {
            try
            {
                if (ABSCommonLibrary.ShowQuestion("Ban muon them moi gia dich " +
                    cboTransType.Text.ToLower() + " cua tai khoan " + txtAccountNo.Value +
                    " so tien " + nbrTransAmount.Value.ToString(DeclareLibrary.CURRENCY_FORMAT) + "?") ==
                     DialogResult.No)
                    return;
                object[] p_param = new object[8];
                p_param[0] = DeclareLibrary.MARKET_DAY;
                p_param[1] = DeclareLibrary.MARKET_DAY;
                p_param[2] = ((DataRowView)cboTransType.SelectedItem)["CODE"];
                p_param[3] = nbrTransAmount.Value;
                p_param[4] = txtAccountNo.Value;
                p_param[5] = txtNotes.Text; 
                p_param[7] = DeclareLibrary.MARKET_DAY;
                p_param[6] = DeclareLibrary.USER_NAME;
                int error_code = Convert.ToInt32(ABSCommonLibrary.DoExecute(
                    CommonLibrary.CommonLibrary.BOSC014003, ref p_param));
                switch (error_code)
                {
                    case 0:
                        ABSCommonLibrary.ShowMessage("Giao dich " +
                            cboTransType.Text.ToLower() + " cua tai khoan " + txtAccountNo.Value +
                        " so tien " + nbrTransAmount.Value.ToString(DeclareLibrary.CURRENCY_FORMAT) +
                        " da duoc thuc hien");
                        load_grid();
                    break;
                    case -1:
                        ABSCommonLibrary.ShowMessage("Tai khoan nha dau tu khong ton tai");
                        break;
                    case -2:
                        ABSCommonLibrary.ShowMessage("Khong du so du trong tai khoan de rut");
                        break;
                    default:
                        ABSCommonLibrary.ShowMessage("Loi khong xac dinh duoc nguyen nhan");
                        break;
                }
            }
            catch (Exception ex)
            {
                ABSCommonLibrary.ShowErrorMessage(ex.ToString());
            }
        }
    }
}